﻿
var autoFreshTime = 1000; //自动刷新间隔
var startTime = "21:00"; //登车开始时间;买票区间
var endTime = "23:59"; //登车结束时间
arrTrain = []; //存放选择的优先车次
arrSeat = []//存放选择的优先座席
var bStop = false; //停止开关，要停止刷新时在console中输入bStop=true;回车就行,大小写敏感
bStop = false;

/*<li name="商务座" scode="SWZ">商务座</li>
<li name="特等座" scode="TZ">特等座</li>
<li name="一等座" scode="ZY" class="cur">一等座</li>
<li name="二等座" scode="ZE">二等座</li>
<li name="高级软卧" scode="GR">高级软卧</li>
<li name="软卧" scode="RW">软卧</li>
<li name="硬卧" scode="YW">硬卧</li>
<li name="动卧" scode="SRRB">动卧</li>
<li name="高级动卧" scode="YYRW">高级动卧</li>
<li name="软座" scode="RZ">软座</li>
<li name="硬座" scode="YZ">硬座</li>
<li name="无座" scode="WZ">无座</li>*/

function TrainSeat(seatName, seatID) {
	this.name = seatName;
	this.seatID = seatID;

}
function Train(trainName, trainID) {
	this.trainName = trainName;
	this.trainID = trainID;
}
//查找优先车次ID;
$("#prior_train > span").each(function (index, element) {
	// console.log(index);
	// console.log(element);
	if (index > 1) {
		var train = new Train(element.innerText, 0);
		//console.log(train);
		var arrChild = $("#queryLeftTable tr:visible");
		for (var i = 0; i < arrChild.length; i++) {

			if (train.trainName > "" && arrChild[i].id.indexOf(train.trainName) > 0) {
				train.trainID = arrChild[i].id;
				arrTrain.push(train);
				break;
			}
		}
	}
})
//如果车次为空，根据登车时间选取优先车次

if (arrTrain.length == 0) {
	//).find(".start-t")[0].innerText
	$("#queryLeftTable tr:visible").each(function (index, element) {
		//console.log($(element).find(".start-t")[0].innerText);
		if ($(element).find(".start-t")[0]) {
			if (comptime($(element).find(".start-t")[0].innerText, startTime) > 0 &&
				comptime($(element).find(".start-t")[0].innerText, endTime) < 0) {
				var train = new Train(element.innerText, 0);
				train.trainID = element.id;
				arrTrain.push(train);
			}
		}
	});
}
//比较登车时间
function comptime(a, b) {
	var dateA = new Date("1900/1/1 " + a);
	var dateB = new Date("1900/1/1 " + b);
	if (isNaN(dateA) || isNaN(dateB))
		return null;
	if (dateA > dateB)
		return 1;
	if (dateA < dateB)
		return -1;
	return 0;
}
//查找优先座位ID
$("#prior_seat > span").each(function (index, element) {
	if (index > 0) {
		var ts = new TrainSeat(element.innerText, 0);
		switch (element.innerText) {
		case "商务座":
			ts.seatID = "SWZ";
			break;
		case "特等座":
			ts.seatID = "TZ";
			break;
		case "一等座":
			ts.seatID = "ZY" ;
			break;
		case "二等座":
			ts.seatID = "ZE";
			break;
		case "高级软卧":
			ts.seatID = "GR";
			break;
		case "软卧":
			ts.seatID = "RW";
			break;
		case "硬卧":
			ts.seatID = "YW";
			break;
		case "动卧":
			ts.seatID = "SRRB";
			break;
		case "高级动卧":
			ts.seatID = "YYRW";
			break;
		case "软座":
			ts.seatID = "RZ";
			break;
		case "硬座":
			ts.seatID = "YZ";
			break;
		case "无座":
			ts.seatID = "WZ";
			break;
		}
		arrSeat.push(ts);
	}

})
//开始自动刷新

timer1 = setInterval(function () {
		if (bStop) {
			clearInterval(timer1)
			return
		}
		$("#query_ticket").html("查询");

		$("#query_ticket").trigger("click");
	}, autoFreshTime);

//监控想买的票，有票则立即停止刷新
var callback = function (records) {
	records.map(function (record) {
		// console.log('Mutation type: ' + record.type);
		// console.log('Mutation target: ' + record.target);
		if (bStop) {
			mo.disconnect();
		}

		for (var i = 0; i < arrTrain.length; i++) {

			//var atr = $(arrTrain[i].trainID);
			for (var j = 0; j < arrSeat.length; j++) {

				var aSeat = $("#" + arrTrain[i].trainID.replace("ticket", arrSeat[j].seatID));
				if (aSeat.length>0) {
					console.log(aSeat.text());
					if (aSeat.text() != "" && aSeat.text() != "无" && aSeat.text() != "*" && aSeat.text() != "--"
					&& typeof(aSeat.text() )!="undefined") {
						clearInterval(timer1);
						mo.disconnect();
						console.log("自动查询终止："+aSeat.text());
						bStop = true;	
						var btn = $($("#" + arrTrain[i].trainID).find("td")[12]).children("a")
							if (btn.length>0) {
								$(btn).trigger("click");
							}
							//btn.dispatchEvent(evt);
					}
					//	console.log(arrTrain[i].trainName + ' ' + arrSeat[j].name + ' ' + aSeat.innerText);
				}
				// console.log($("#"+arrTrain[i].trainID+" td:nth-child("+arrSeat[j].colIndex+")").innerText);
				//$(atr).find("td")[arrSeat.colIndex].innerText<>"--"
			}

		};
	});
}

mo = new MutationObserver(callback);

var option = {
	'childList' : true,
	'subtree' : true
};

mo.observe(document.getElementById("t-list"), option);
